/*
 * @(#) $Header$
 *
 * Copyright (C)  2010  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.baselib.util.predicate;

import java.util.LinkedList;
import java.util.List;
import ca.forklabs.baselib.util.Iterators;
import ca.forklabs.baselib.util.UnaryPredicate;

/**
 * An {@code UnaryAnd} performs a short-circuit "and" over an ordered
 * list of {@link UnaryPredicate}s. The predicates are evaluated in the order
 * they are given.
 *
 * @param   <A>   the type of the argument object.
 *
 * @author <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.baselib.util.predicates.UnaryAnd">Daniel Léonard</a>
 * @version $Revision$
 */
public class UnaryAnd<A> implements UnaryPredicate<A> {

//---------------------------
// Instance variables
//---------------------------

   /** The list of predicates. */
   private List<UnaryPredicate<A>> predicates = new LinkedList<>();


//---------------------------
// Constructors
//---------------------------

   /**
    * Constructor.
    * @param   predicates   the predicates to "and" together.
    * @exception   NullPointerException   if the parameter is <code>null</code>.
    */
   @SafeVarargs
   public UnaryAnd(UnaryPredicate<A>... predicates) {
      this(Iterators.asIterable(predicates));
      }

   /**
    * Constructor.
    * @param   predicates   the predicates to "and" together.
    * @exception   NullPointerException   if the parameter is <code>null</code>.
    */
   public UnaryAnd(Iterable<UnaryPredicate<A>> predicates) {
      this.setup(predicates);
      }


//---------------------------
// Accessors and mutators
//---------------------------

   /**
    * Gets the list of predicates.
    * @return   the list of predicates.
    */
   protected List<UnaryPredicate<A>> getPredicates() {
      return this.predicates;
      }


//---------------------------
// Accessors and mutators
//---------------------------

   /**
    * Sets up the list of predicates.
    * @param   predicates   the predicates.
    */
   protected void setup(Iterable<UnaryPredicate<A>> predicates) {
      if (null == predicates) {
         String message = this.getNullPredicatesErrorMessage();
         throw new NullPointerException(message);
         }

      List<UnaryPredicate<A>> list = this.getPredicates();
      for (UnaryPredicate<A> predicate : predicates) {
         list.add(predicate);
         }
      }


//---------------------------
// Implemented methods from ca.forklabs.baselib.util.UnaryPredicate
//---------------------------

   /**
    * Performs a short-circuit "and" with all the predicates.
    * @param   a   the object to evaluate.
    * @return   the evaluation.
    */
   @Override
   @SuppressWarnings("hiding")
   public boolean eval(A a) {
      boolean evaluation = true;

      List<UnaryPredicate<A>> predicates = this.getPredicates();
      for (UnaryPredicate<A> predicate : predicates) {
         evaluation = evaluation && predicate.eval(a);
         if (false == evaluation) {
            break;
            }
         }

      return evaluation;
      }


//---------------------------
// Instance methods
//---------------------------

   /**
    * Gets the error message saying that the list of predicates is {@code null}.
    * @return   the error message.
    */
   protected String getNullPredicatesErrorMessage() {
      String key = Resources.NULL_PREDICATES;
      String message = Resources.getLocalizedString(key);
      return message;
      }

   }
